var app = getApp();
//声明全局变量
let proListToTop = [], menuToTop = [], MENU = 0, windowHeight, timeoutId;
// MENU ==> 是否为点击左侧进行滚动的，如果是，则不需要再次设置左侧的激活状态
const {
    getGoodsClass
} = require("../../api/index")
Page({

    data: {
        currentActiveIndex: 0,
        // 接口返回的商品数组
        navList: [
            {
                c_id: "01",
                c_name: '电脑办公',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "02",
                c_name: '祛痘',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "02",
                c_name: '化妆品',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "03",
                c_name: '口红',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "04",
                c_name: '包包',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "05",
                c_name: '面膜',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "06",
                c_name: '精华乳',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "07",
                c_name: '洁面乳',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "08",
                c_name: '洁面乳',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "09",
                c_name: '洁面乳',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "10",
                c_name: '洁面乳',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "11",
                c_name: '洁面乳',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "12",
                c_name: '洁面乳',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "13",
                c_name: '洁面乳',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "14",
                c_name: '洁面乳',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "07",
                c_name: '洁面乳',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
            {
                c_id: "07",
                c_name: '洁面乳',
                list: [
                    {
                        id: 1,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 2,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 3,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 4,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                    {
                        id: 5,
                        url: 'https://resource.yirenheju.cn/wechat/index/err.png',
                        goodsName: '玩具',
                    },
                ]
            },
        ],
    },
    onLoad: function (e) {
        getGoodsClass().then(res => {
        // console.log(res,'234567890');
        let newArr = [];
        //获取一级数据
        newArr = res.data.data.filter((item) => item.level === "1")
        //从一级数据里获取二级数据children
        newArr = newArr.map((item) => {
          return {
            ...item,
            children: res.data.data.filter((val) => val.parentCode === item.code),
          }
        })
        //从二级数据里获取三级数据
        newArr = newArr.map((item) => {
          return {
            ...item,
            children: item.children.map((val) => {
              return {
                ...val,
                children: res.data.data.filter((v) => v.parentCode === val.code)
              }
            })
          }
        })
        this.setData({
          // 将数据赋给原定义的空数组
          navList: [...newArr]
        })
      })
        // 确保页面数据已经刷新完毕~
        setTimeout(() => {
            this.getAllRects()
        }, 20)
    },
    changeMenu(e) {
        // console.log(proListToTop);
        // 改变左侧tab栏操作
        if (Number(e.target.id) === this.data.currentActiveIndex) return
        MENU = 1
        this.setData({
            currentActiveIndex: Number(e.target.id),
            rightProTop: proListToTop[Number(e.target.id)]
        })
    },
    scroll(e) {
        // console.log(e);
        for (let i = 0; i < proListToTop.length; i++) {
            if (e.detail.scrollTop < proListToTop[i] && i !== 0 && e.detail.scrollTop > proListToTop[i - 1]) {
                return this.setDis(i)
            }
        }
        // 找不到匹配项，默认显示第一个数据
        if (!MENU) {
            this.setData({
                currentActiveIndex: 0
            })
        }
        MENU = 0
    },
    setDis(i) {
        // 设置左侧menu栏的选中状态
        if (i !== this.data.currentActiveIndex + 1 && !MENU) {
            this.setData({
                currentActiveIndex: i - 1
            })
        }
        MENU = 0
    },
   
    getAllRects() {
        // 获取商品数组的位置信息
        wx.createSelectorQuery().selectAll('.pro-item').boundingClientRect(function (rects) {
            rects.forEach(function (rect) {
                // console.log(rect)
                // 这里减去44是根据你的滚动区域距离头部的高度，如果没有高度，可以将其删去
                proListToTop.push(rect.top)
            })
        }).exec()
    }

})